package servlet;

import util.*;

import java.io.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.*;
import javax.servlet.http.*;

public class Insert extends HttpServlet{



    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取客户端请求的参数，并进行判断。
        request.setCharacterEncoding("utf-8");
        //String id=request.getParameter("id").trim();//取得用户名参数
        String name=request.getParameter("name").trim();//取得用户密码参数
        String sex=request.getParameter("sex").trim();
        String birthday=request.getParameter("birth").trim();
        String education=request.getParameter("degree").trim();
        String native_place=request.getParameter("origin").trim();
        String place=request.getParameter("address").trim();
        String phone=request.getParameter("tel").trim();
        double work_age=Double.parseDouble(request.getParameter("wyear").trim());
        double base_pay=Double.parseDouble(request.getParameter("wage").trim());
        try{
                    DButil dButil = new DButil();
                    Connection con = dButil.getConnection();
                    String sql = "INSERT INTO staff(name,sex,birth,degree,origin,address,tel,wyear,wage) VALUES (?,?,?,?,?,?,?,?,?)";

                    PreparedStatement ps = con.prepareStatement(sql);

                  
                    //ps.setString(1, id);
                    ps.setString(1, name);
                    ps.setString(2, sex);
                    ps.setString(3, birthday);
                    ps.setString(4, education);
                    ps.setString(5, native_place);
                    ps.setString(6, place);
                    ps.setString(7, phone);
                    ps.setDouble(8, work_age);
                    ps.setDouble(9, base_pay);

                   if (ps.executeUpdate() != 0){
                       RequestDispatcher dispatcher =
                               request.getRequestDispatcher("index.jsp");//转发
                       dispatcher.forward(request, response);
                   }
                   else
                   {
                       RequestDispatcher dispatcher=
                               request.getRequestDispatcher("insert.jsp");//转发
                       dispatcher.forward(request,response);
                   }
        }catch(Exception e){
            e.printStackTrace();

        }


    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
